<template>
    <div class="goodsinfo-container">

        <!-- 加入购物车小球动画 -->
        <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
            <div class="ball" ref="ball" v-show="ballFlag"></div>
        </transition>

        <!-- 轮播图 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
                </div> 
            </div>
        </div>

        <!-- 商品购买区域 -->
        <div class="mui-card">
            <div class="mui-card-header">{{goodsinfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="price">
                        市场价：<del>￥{{ goodsinfo.market_price }}</del>&nbsp;&nbsp;
                        销售价：<span class="now_price">￥{{ goodsinfo.sell_price }}</span>
                    </div>
                                        <!-- 子组件向父组件传值(客户要购买的数量)   最大购买量(库存)   -->
                    <div class="shopcount">购买数量：<numbox @getCount="getSelectedCount"  :max="goodsinfo.stock_quantity"></numbox></div>
                    <div>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addShopCar">加入购物车</mt-button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 商品详情区域 -->
         <div class="mui-card">
            <div class="mui-card-header"><strong>商品详情</strong></div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner detail">
                    <div>商品货号 : {{ goodsinfo.goods_no }}</div>
                    <div>商品库存 : {{ goodsinfo.stock_quantity }}</div>
                    <div>上架时间 : {{ goodsinfo.add_time | dateFormat("YYYY-MM-DD") }}</div>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" plain size="large" @click="getDesc(id)">图文介绍</mt-button>

                <mt-button type="danger" size="large" plain @click="getComment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
import swiper from '../subcomponents/Swiper.vue';
import numbox from '../subcomponents/goodsinfo_numbox.vue';
export default {
    data() {
        return {
            id: this.$route.params.id,  // 商品ID
            lunbotu: [],   // 轮播图
            goodsinfo: {},  // 商品详情信息
            ballFlag: false, // 小球显示与否的标记
            selectedCount: 1   // 客户选中的商品数量  默认为 1
        }
    },

    created () {
        this.getGoodsInfoLunBoTu(this.id),
        this.getGoodsDetailInfo(this.id) 
    },

    methods: {
        // 获取轮播图数据
        getGoodsInfoLunBoTu(id) {
            this.$http.get('api/getthumimages/' + this.id).then(res=>{
                if (res.body.status === 0) {
                    res.body.message.forEach(item => {
                        item.img = item.src
                    });
                    this.lunbotu = res.body.message
                }
            })
        },
        // 获取商品详情
        getGoodsDetailInfo(id) {
            this.$http.get('api/goods/getinfo/' + this.id).then(res=>{
                if (res.body.status === 0) {
                   this.goodsinfo = res.body.message[0] 
                }
            })
        },
        // 进入图文描述
        getDesc(id){
            this.$router.push({name: 'goodsdesc', params:{id}})
        },
        // 进入评论列表
        getComment(id){
            this.$router.push({name: 'goodscomment', params:{id}})
        },
        // 添加到购物车
        addShopCar(){
            this.ballFlag = !this.ballFlag;
            var goodsInfo = {
                id: this.id,
                count: this.selectedCount,
                price: this.goodsinfo.sell_price,
                selected: true
            }
            this.$store.commit('addCar',goodsInfo);
            
        },

        // 小球动画的钩子函数
        beforeEnter(el){
            el.style.transform = "translate(0,0)";
        },
        enter(el,done){
            el.offsetWidth;  // 这一步有了 才会显示动画效果
            // 获取小球和购物在页面上的动态位置
            const ballPosition = this.$refs.ball.getBoundingClientRect();
            const badgePosition = document.getElementById('badge').getBoundingClientRect();
            // 小球要移动的 x y轴距离
            const xDist = badgePosition.left - ballPosition.left;
            const yDist = badgePosition.top - ballPosition.top;
            
             el.style.transform = `translate(${xDist}px,${yDist}px)`;
             el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";  //cubic-bezier() 运动曲线

            done();
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag;
        },

        // 子组件向父组件传递 客户选定的商品数量
        getSelectedCount(count){
            this.selectedCount = count;
            // console.log(this.selectedCount);
        }


    },

    components: {
        swiper,
        numbox
    }
}

</script>


<style lang="less" scoped>

    .goodsinfo-container {
        background-color: #eee;
        overflow: hidden;

        .shopcount {
            padding: 10px 0;
        }
        .now_price {
            color: red;
            font-size: 16px;
            font-weight: bold;
        }

        .mui-card-footer {
            display: block;
            button {
            margin: 15px 0;
            }
        }

        .detail {
            div {
                padding: 5px 0;
            }
        }

        .ball {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            background-color: red;
            position: absolute;
            z-index: 99;
            top: 390px;
            left: 146px;
        }

    }
</style>
